import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zyj
 * @date 2022年06月10日 16:44
 */
public class Server {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8080);
            System.out.println("服务端已经启动，等待客户端连接");
            Socket socket = server.accept();
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
            boolean flag = true;
            while (flag){
                //获取客户端发送的消息
                String str = reader.readLine();
                System.out.println("客户端发的消息：" + str);
                if (null == str || "".equals(str)){
                    flag = false;
                }else {
                    if ("exit".equals(str)){
                        flag = false;
                    }else {
                        System.out.println("请输入发送给客户端的消息");
                        writer.write(input.readLine());
                        writer.newLine();
                        writer.flush();
                    }
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
